function getGoodId(){
    $.ajax({
        url: "/goods/getGoodId",
        success(response){
            if (response != null && response != ''){
                var id = document.getElementById("id")
                var variety = document.getElementById("variety");
                var price = document.getElementById("price");
                id.value = response.id
                variety.value = response.name
                price.value = response.price
            }
        }
    })
}
function submitData() {
    var id = document.getElementById("id");
    var variety = document.getElementById("variety");
    var price = document.getElementById("price");
    if (id.value != 0 && id.value != null && id.value && variety.value != null && variety.value != '' && price.value != "" && price.value != null){
        id = id.value
        name = variety.value
        price = price.value
        $.ajax({
            url: '/goods/update',
            data: {id,name,price},
            success(response) {
                if (response > 0){
                    location.href = '/?target=good-list'
                }
            }
        })
    }else if (variety.value != null && variety.value != '' && price.value != "" && price.value != null){
        saveGood(variety.value,price.value)
    }
    location.href = '/?target=good-list'
}

function saveGood(name, price) {
    $.ajax({
        url: "/goods/save",
        data: {name,price},
        success(res){
            console.log("成功")
        }
    })
}

window.addEventListener("load", function () {
    getGoodId()
})